use "$working/results/bs_welfare", clear

keep *_N2* *_N3* bs diff_p_hat_it_b2_y diff_p_hat_it_b2_yn diff_p_hat_it_b2_ycn ///
	diff_p_hat_it_b3_y diff_p_hat_it_b3_yn diff_p_hat_it_b3_ycn ///
	diff_q_hat_it_b2_y diff_q_hat_it_b2_yn diff_q_hat_it_b2_ycn ///	
	diff_q_hat_it_b3_y diff_q_hat_it_b3_yn diff_q_hat_it_b3_ycn 
save "$working/temp/temp_w", replace

use "$working/results/bs_decomposition_SS_ProdR_new_kl", clear

keep *_N2* *_N3* bs dif_d_bar_b2_y dif_d_bar_b2_yn dif_d_bar_b2_ycn ///
	dif_d_bar_b3_y dif_d_bar_b3_yn dif_d_bar_b3_ycn ///
	dif_a_bar_b2_y dif_a_bar_b2_yn dif_a_bar_b2_ycn ///
	dif_a_bar_b3_y dif_a_bar_b3_yn dif_a_bar_b3_ycn ///
	diff_tfpr_b2_y diff_tfpr_b2_yn diff_tfpr_b2_ycn ///
	diff_tfpr_b3_y diff_tfpr_b3_yn diff_tfpr_b3_ycn
merge 1:1 bs using "$working/temp/temp_w"
drop _merge
drop *_cov_stay_* *var_adj* *adding_term* *drop_term* dif_d_bar_* dif_a_bar_*
	preserve
		drop if bs == 0 | bs == -1
		drop *N2_* *N3_*
		collapse (sd) *
		replace bs = 0
		reshape long diff_tfpr_  diff_q_hat_it_ diff_p_hat_it_ , i(bs) j(FE) string
		gen fe = 1 if FE == "b2_y"
		replace fe = 2 if FE == "b2_ycn"
		replace fe = 3 if FE == "b2_yn"
		replace fe = 4 if FE == "b3_y"
		replace fe = 5 if FE == "b3_ycn"
		replace fe = 6 if FE == "b3_yn"
		xpose, clear varname
		rename v1 b2_y
		rename v2 b2_ycn
		rename v3 b2_yn
		rename v4 b3_y
		rename v5 b3_ycn
		rename v6 b3_yn
		gen bs = 2
		drop if _varname == "bs" | _varname == "FE" | _varname == "fe"
		save "$working/temp/bstemp", replace
	restore
	
	preserve
		drop *N2_* *N3_*
		keep if bs == -1
		reshape long diff_tfpr_ diff_q_hat_it_ diff_p_hat_it_ , i(bs) j(FE) string
		xpose, clear varname
		rename v1 b2_y
		rename v2 b2_ycn
		rename v3 b2_yn
		rename v4 b3_y
		rename v5 b3_ycn
		rename v6 b3_yn
		gen bs = 1
		drop if _varname == "bs" | _varname == "FE"
		save "$working/temp/bstemp2", replace
	restore
	
	keep if bs == 0
	rename diff_p_hat_it_N3_y obs
	drop *_N*
		reshape long diff_tfpr_ diff_q_hat_it_ diff_p_hat_it_ , i(bs) j(FE) string
		xpose, clear varname
		rename v1 b2_y
		rename v2 b2_ycn
		rename v3 b2_yn
		rename v4 b3_y
		rename v5 b3_ycn
		rename v6 b3_yn
		gen bs = 0
		drop if _varname == "bs" | _varname == "FE"

	append using "$working/temp/bstemp"
	append using "$working/temp/bstemp2"
	gen varorder = 1 if _varname == "diff_p_hat_it_"
	replace varorder = 2 if _varname == "diff_tfpr_"
	replace varorder = 3 if _varname == "diff_q_hat_it_"
sort varorder bs

gen var = "$\Delta \ln Z_{it} $" if _varname == "diff_tfpr_" & bs == 0
replace var = "$\Delta \ln \mathcal{P}_{it}$" if _varname == "diff_p_hat_it_" & bs == 0
replace var = "$\Delta \ln \mathcal{Q}_{it}$" if _varname == "diff_q_hat_it_" & bs == 0
replace var = "Observations" if _varname == "obs"

foreach v in b2_y b2_ycn b2_yn b3_y b3_ycn b3_yn {
	tostring `v', replace format(%9.3f) force
	replace `v' = "(" + `v' + ")" if bs == 1
	replace `v' = "[" + `v' + "]" if bs ==2
}

insobs 1
replace var = "Year FE" if missing(bs)
replace bs = 3 if missing(bs)
insobs 1
replace var = "Industry FE" if missing(bs)
replace bs = 3 if missing(bs)
insobs 1
replace var = "Industry-Year FE" if missing(bs)
replace bs = 3 if missing(bs)
foreach v in b2_y b3_y {
	replace `v' = "Yes" if var == "Year FE"
	replace `v' = "No" if var == "Industry FE"
	replace `v' = "No" if var == "Industry-Year FE"
}

foreach v in b2_yn b3_yn {
	replace `v' = "Yes" if var == "Year FE"
	replace `v' = "Yes" if var == "Industry FE"
	replace `v' = "No" if var == "Industry-Year FE"
}

foreach v in b2_ycn b3_ycn {
	replace `v' = "No" if var == "Year FE"
	replace `v' = "No" if var == "Industry FE"
	replace `v' = "Yes" if var == "Industry-Year FE"
}
drop _varname bs
order var b2_y b2_yn b2_ycn b3_y b3_yn b3_ycn

	texsave var b2_y b2_yn b2_ycn b3_y b3_yn b3_ycn using "$tables/welfare_regs.tex", ///
		footnote("Notes: Estimates of (\ref{eq: causal-specification}) where the outcome of interest is i)  the first difference in the log firm-level price index (first row) ii) the first difference in log revenue based TFP (fourth row), and iii) the first difference the log firm-level quantity index. Further specification details described in the text. Analytical standard errors (clustered by 4-digit industry) in parentheses. Bootstrapped standard errors in brackets.") ///
		title("Welfare regressions") ///
		nofix replace location(h) varlab marker(Table:Welfare) frag ///
		autonumber width(\textwidth) hlines(9)

/*
Add this to the tex file to fix headings: 
&\multicolumn{3}{c}{Instrumenting InputAccess}   &\multicolumn{3}{c}{InputAccessIV as Control}\\\cmidrule(lr){2-4}\cmidrule(lr){5-7}
*/

generate c_order = _n

keep if c_order == 1 | c_order==4 | c_order==7

destring b*, replace

generate delta = 0.822
generate share = 0.65*0.15*0.15
generate D_China = 2.93


foreach variable in  b2_y b2_yn b2_ycn b3_y b3_yn b3_ycn {
	replace	`variable' = -1*`variable' if c_order == 1 // This is price, which has a negative coefficient; to turn into quantity units, you multiply by -1 as in (G.4) 
	generate `variable'_new = ((exp(delta*D_China*`variable')*share+(1-share))^(1/delta) - 1)*100
	tostring `variable'_new, replace format(%9.3f) force
	replace `variable'_new = `variable'_new + "\%"
}


texsave var b2_y_new b2_yn_new b2_ycn_new b3_y_new b3_yn_new b3_ycn_new using "$tables/welfare_CF.tex", ///
		footnote("Notes: Calculates the implied percentage increase in welfare using equations (\ref{eq:welfare decomp}) and (\ref{eq: welfare_formula}), if the decomposition samples reacted to a log change in Chinese imports of 2.93 according to the coefficients in Table \ref{Table:Welfare}. See text for further details.") ///
		nofix replace location(h) varlab marker(Table:WelfareCounter) frag ///
		autonumber width(\textwidth) hlines(3)
